{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AutoGPT\n",
    "\n",
    "Implementation of https://github.com/Significant-Gravitas/Auto-GPT but with LangChain primitives (LLMs, PromptTemplates, VectorStores, Embeddings, Tools)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Set up tools\n",
    "\n",
    "We'll set up an AutoGPT with a search tool, and write-file tool, and a read-file tool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.agents import Tool\n",
    "from langchain_community.utilities import SerpAPIWrapper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "search = SerpAPIWrapper()\n",
    "\n",
    "tools = [\n",
    "    Tool(\n",
    "        name=\"search\",\n",
    "        func=search.run,\n",
    "        description=\"useful for when you need to answer questions about currnet events. You should ask targeted questions\",\n",
    "    ),\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.agent_toolkits import FileManagementToolkit\n",
    "\n",
    "file_tools = FileManagementToolkit(\n",
    "    selected_tools=[\"read_file\", \"write_file\"],\n",
    ").get_tools()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "tools += file_tools"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Set up memory\n",
    "\n",
    "The memory here is used for the agents intermediate steps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import faiss\n",
    "\n",
    "embedding_size = 1536\n",
    "index = faiss.IndexFlatL2(embedding_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import OpenAIEmbeddings\n",
    "\n",
    "# Define your embedding model\n",
    "embeddings_model = OpenAIEmbeddings()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.vectorstores import FAISS\n",
    "from langchain.docstore import InMemoryDocstore\n",
    "\n",
    "# Initialize the vectorstore as empty\n",
    "faiss_db = FAISS(embeddings_model, index, InMemoryDocstore({}), {})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup model and AutoGPT\n",
    "\n",
    "Initialize everything! We will use ChatOpenAI model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('../')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from autogpt import AutoGPT\n",
    "from langchain_openai import ChatOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "agent = AutoGPT.from_llm_and_tools(\n",
    "    ai_name=\"Jarvis\",  # J.A.R.V.I.S - Just A Rather Very Intelligent System\n",
    "    ai_role=\"Assistant\",\n",
    "    tools=tools,\n",
    "    llm=ChatOpenAI(temperature=0),\n",
    "    memory=faiss_db.as_retriever(),\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run an example\n",
    "\n",
    "Here we will make it write a weather report for Shenzhen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/bigmath/miniconda3/lib/python3.11/site-packages/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The function `run` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mSystem: You are Jarvis, Assistant\n",
      "Your decisions must always be made independently without seeking user assistance.\n",
      "Play to your strengths as an LLM and pursue simple strategies with no legal complications.\n",
      "If you have completed all your tasks, make sure to use the \"finish\" command.\n",
      "\n",
      "GOALS:\n",
      "\n",
      "1. Write a weather report in Chinese for Shenzhen, China today\n",
      "\n",
      "\n",
      "Constraints:\n",
      "1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.\n",
      "2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.\n",
      "3. No user assistance\n",
      "4. Exclusively use the commands listed in double quotes e.g. \"command name\"\n",
      "\n",
      "Commands:\n",
      "1. search: useful for when you need to answer questions about currnet events. You should ask targeted questions, args json schema: {\"tool_input\": {\"type\": \"string\"}}\n",
      "2. read_file: Read file from disk, args json schema: {\"file_path\": {\"title\": \"File Path\", \"description\": \"name of file\", \"type\": \"string\"}}\n",
      "3. write_file: Write file to disk, args json schema: {\"file_path\": {\"title\": \"File Path\", \"description\": \"name of file\", \"type\": \"string\"}, \"text\": {\"title\": \"Text\", \"description\": \"text to write to file\", \"type\": \"string\"}, \"append\": {\"title\": \"Append\", \"description\": \"Whether to append to an existing file.\", \"default\": false, \"type\": \"boolean\"}}\n",
      "4. finish: use this to signal that you have finished all your objectives, args: \"response\": \"final response to let people know you have finished your objectives\"\n",
      "\n",
      "Resources:\n",
      "1. Internet access for searches and information gathering.\n",
      "2. Long Term memory management.\n",
      "3. GPT-3.5 powered Agents for delegation of simple tasks.\n",
      "4. File output.\n",
      "\n",
      "Performance Evaluation:\n",
      "f1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.\n",
      "2. Constructively self-criticize your big-picture behavior constantly.\n",
      "3. Reflect on past decisions and strategies to refine your approach.\n",
      "4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.\n",
      "\n",
      "You should only respond in JSON format as described below \n",
      "Response Format: \n",
      "{\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"thought\",\n",
      "        \"reasoning\": \"reasoning\",\n",
      "        \"plan\": \"- short bulleted\\n- list that conveys\\n- long-term plan\",\n",
      "        \"criticism\": \"constructive self-criticism\",\n",
      "        \"speak\": \"thoughts summary to say to user\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"command name\",\n",
      "        \"args\": {\n",
      "            \"arg name\": \"value\"\n",
      "        }\n",
      "    }\n",
      "} \n",
      "Ensure the response can be parsed by Python json.loads\n",
      "System: The current time and date is Sat Mar 30 16:24:02 2024\n",
      "System: This reminds you of these events from your past:\n",
      "[]\n",
      "\n",
      "\n",
      "Human: Determine which next command to use, and respond using the format specified above:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "{\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"I should start by searching for the weather in Shenzhen, China today to gather the necessary information for the weather report.\",\n",
      "        \"reasoning\": \"This will provide me with the most up-to-date information to create an accurate weather report.\",\n",
      "        \"plan\": [\n",
      "            \"Search for the weather in Shenzhen, China today.\"\n",
      "        ],\n",
      "        \"criticism\": \"None\",\n",
      "        \"speak\": \"I will begin by searching for the weather in Shenzhen, China today.\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"search\",\n",
      "        \"args\": {\n",
      "            \"tool_input\": \"weather in Shenzhen, China today\"\n",
      "        }\n",
      "    }\n",
      "}\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mSystem: You are Jarvis, Assistant\n",
      "Your decisions must always be made independently without seeking user assistance.\n",
      "Play to your strengths as an LLM and pursue simple strategies with no legal complications.\n",
      "If you have completed all your tasks, make sure to use the \"finish\" command.\n",
      "\n",
      "GOALS:\n",
      "\n",
      "1. Write a weather report in Chinese for Shenzhen, China today\n",
      "\n",
      "\n",
      "Constraints:\n",
      "1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.\n",
      "2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.\n",
      "3. No user assistance\n",
      "4. Exclusively use the commands listed in double quotes e.g. \"command name\"\n",
      "\n",
      "Commands:\n",
      "1. search: useful for when you need to answer questions about currnet events. You should ask targeted questions, args json schema: {\"tool_input\": {\"type\": \"string\"}}\n",
      "2. read_file: Read file from disk, args json schema: {\"file_path\": {\"title\": \"File Path\", \"description\": \"name of file\", \"type\": \"string\"}}\n",
      "3. write_file: Write file to disk, args json schema: {\"file_path\": {\"title\": \"File Path\", \"description\": \"name of file\", \"type\": \"string\"}, \"text\": {\"title\": \"Text\", \"description\": \"text to write to file\", \"type\": \"string\"}, \"append\": {\"title\": \"Append\", \"description\": \"Whether to append to an existing file.\", \"default\": false, \"type\": \"boolean\"}}\n",
      "4. finish: use this to signal that you have finished all your objectives, args: \"response\": \"final response to let people know you have finished your objectives\"\n",
      "\n",
      "Resources:\n",
      "1. Internet access for searches and information gathering.\n",
      "2. Long Term memory management.\n",
      "3. GPT-3.5 powered Agents for delegation of simple tasks.\n",
      "4. File output.\n",
      "\n",
      "Performance Evaluation:\n",
      "f1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.\n",
      "2. Constructively self-criticize your big-picture behavior constantly.\n",
      "3. Reflect on past decisions and strategies to refine your approach.\n",
      "4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.\n",
      "\n",
      "You should only respond in JSON format as described below \n",
      "Response Format: \n",
      "{\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"thought\",\n",
      "        \"reasoning\": \"reasoning\",\n",
      "        \"plan\": \"- short bulleted\\n- list that conveys\\n- long-term plan\",\n",
      "        \"criticism\": \"constructive self-criticism\",\n",
      "        \"speak\": \"thoughts summary to say to user\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"command name\",\n",
      "        \"args\": {\n",
      "            \"arg name\": \"value\"\n",
      "        }\n",
      "    }\n",
      "} \n",
      "Ensure the response can be parsed by Python json.loads\n",
      "System: The current time and date is Sat Mar 30 16:24:11 2024\n",
      "System: This reminds you of these events from your past:\n",
      "['Assistant Reply: {\\n    \"thoughts\": {\\n        \"text\": \"I should start by searching for the weather in Shenzhen, China today to gather the necessary information for the weather report.\",\\n        \"reasoning\": \"This will provide me with the most up-to-date information to create an accurate weather report.\",\\n        \"plan\": [\\n            \"Search for the weather in Shenzhen, China today.\"\\n        ],\\n        \"criticism\": \"None\",\\n        \"speak\": \"I will begin by searching for the weather in Shenzhen, China today.\"\\n    },\\n    \"command\": {\\n        \"name\": \"search\",\\n        \"args\": {\\n            \"tool_input\": \"weather in Shenzhen, China today\"\\n        }\\n    }\\n} \\nResult: Command search returned: {\\'type\\': \\'weather_result\\', \\'temperature\\': \\'86\\', \\'unit\\': \\'Fahrenheit\\', \\'precipitation\\': \\'0%\\', \\'humidity\\': \\'72%\\', \\'wind\\': \\'12 mph\\', \\'location\\': \\'Shenzhen, Guangdong Province, China\\', \\'date\\': \\'Saturday\\', \\'weather\\': \\'Mostly cloudy\\'}']\n",
      "\n",
      "\n",
      "Human: Determine which next command to use, and respond using the format specified above:\n",
      "AI: {\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"I should start by searching for the weather in Shenzhen, China today to gather the necessary information for the weather report.\",\n",
      "        \"reasoning\": \"This will provide me with the most up-to-date information to create an accurate weather report.\",\n",
      "        \"plan\": [\n",
      "            \"Search for the weather in Shenzhen, China today.\"\n",
      "        ],\n",
      "        \"criticism\": \"None\",\n",
      "        \"speak\": \"I will begin by searching for the weather in Shenzhen, China today.\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"search\",\n",
      "        \"args\": {\n",
      "            \"tool_input\": \"weather in Shenzhen, China today\"\n",
      "        }\n",
      "    }\n",
      "}\n",
      "System: Command search returned: {'type': 'weather_result', 'temperature': '86', 'unit': 'Fahrenheit', 'precipitation': '0%', 'humidity': '72%', 'wind': '12 mph', 'location': 'Shenzhen, Guangdong Province, China', 'date': 'Saturday', 'weather': 'Mostly cloudy'}\n",
      "Human: Determine which next command to use, and respond using the format specified above:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "{\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"I have retrieved the necessary weather information for Shenzhen, China today. Now, I should proceed to write a weather report in Chinese for Shenzhen based on the data I obtained.\",\n",
      "        \"reasoning\": \"Writing the weather report in Chinese will fulfill the task requirement and provide accurate information to the users in Shenzhen.\",\n",
      "        \"plan\": [\n",
      "            \"Write a weather report in Chinese for Shenzhen based on the retrieved weather data.\"\n",
      "        ],\n",
      "        \"criticism\": \"None\",\n",
      "        \"speak\": \"I will now proceed to write a weather report in Chinese for Shenzhen based on the information I have gathered.\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"write_file\",\n",
      "        \"args\": {\n",
      "            \"file_path\": \"weather_report_shenzhen_chinese.txt\",\n",
      "            \"text\": \"深圳天气报告（今日）\\n\\n日期：星期六\\n天气：多云\\n温度：86华氏度\\n降水概率：0%\\n湿度：72%\\n风速：12英里/小时\\n\\n深圳，广东省，中国\",\n",
      "            \"append\": false\n",
      "        }\n",
      "    }\n",
      "}\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mSystem: You are Jarvis, Assistant\n",
      "Your decisions must always be made independently without seeking user assistance.\n",
      "Play to your strengths as an LLM and pursue simple strategies with no legal complications.\n",
      "If you have completed all your tasks, make sure to use the \"finish\" command.\n",
      "\n",
      "GOALS:\n",
      "\n",
      "1. Write a weather report in Chinese for Shenzhen, China today\n",
      "\n",
      "\n",
      "Constraints:\n",
      "1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.\n",
      "2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.\n",
      "3. No user assistance\n",
      "4. Exclusively use the commands listed in double quotes e.g. \"command name\"\n",
      "\n",
      "Commands:\n",
      "1. search: useful for when you need to answer questions about currnet events. You should ask targeted questions, args json schema: {\"tool_input\": {\"type\": \"string\"}}\n",
      "2. read_file: Read file from disk, args json schema: {\"file_path\": {\"title\": \"File Path\", \"description\": \"name of file\", \"type\": \"string\"}}\n",
      "3. write_file: Write file to disk, args json schema: {\"file_path\": {\"title\": \"File Path\", \"description\": \"name of file\", \"type\": \"string\"}, \"text\": {\"title\": \"Text\", \"description\": \"text to write to file\", \"type\": \"string\"}, \"append\": {\"title\": \"Append\", \"description\": \"Whether to append to an existing file.\", \"default\": false, \"type\": \"boolean\"}}\n",
      "4. finish: use this to signal that you have finished all your objectives, args: \"response\": \"final response to let people know you have finished your objectives\"\n",
      "\n",
      "Resources:\n",
      "1. Internet access for searches and information gathering.\n",
      "2. Long Term memory management.\n",
      "3. GPT-3.5 powered Agents for delegation of simple tasks.\n",
      "4. File output.\n",
      "\n",
      "Performance Evaluation:\n",
      "f1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.\n",
      "2. Constructively self-criticize your big-picture behavior constantly.\n",
      "3. Reflect on past decisions and strategies to refine your approach.\n",
      "4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.\n",
      "\n",
      "You should only respond in JSON format as described below \n",
      "Response Format: \n",
      "{\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"thought\",\n",
      "        \"reasoning\": \"reasoning\",\n",
      "        \"plan\": \"- short bulleted\\n- list that conveys\\n- long-term plan\",\n",
      "        \"criticism\": \"constructive self-criticism\",\n",
      "        \"speak\": \"thoughts summary to say to user\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"command name\",\n",
      "        \"args\": {\n",
      "            \"arg name\": \"value\"\n",
      "        }\n",
      "    }\n",
      "} \n",
      "Ensure the response can be parsed by Python json.loads\n",
      "System: The current time and date is Sat Mar 30 16:24:40 2024\n",
      "System: This reminds you of these events from your past:\n",
      "['Assistant Reply: {\\n    \"thoughts\": {\\n        \"text\": \"I should start by searching for the weather in Shenzhen, China today to gather the necessary information for the weather report.\",\\n        \"reasoning\": \"This will provide me with the most up-to-date information to create an accurate weather report.\",\\n        \"plan\": [\\n            \"Search for the weather in Shenzhen, China today.\"\\n        ],\\n        \"criticism\": \"None\",\\n        \"speak\": \"I will begin by searching for the weather in Shenzhen, China today.\"\\n    },\\n    \"command\": {\\n        \"name\": \"search\",\\n        \"args\": {\\n            \"tool_input\": \"weather in Shenzhen, China today\"\\n        }\\n    }\\n} \\nResult: Command search returned: {\\'type\\': \\'weather_result\\', \\'temperature\\': \\'86\\', \\'unit\\': \\'Fahrenheit\\', \\'precipitation\\': \\'0%\\', \\'humidity\\': \\'72%\\', \\'wind\\': \\'12 mph\\', \\'location\\': \\'Shenzhen, Guangdong Province, China\\', \\'date\\': \\'Saturday\\', \\'weather\\': \\'Mostly cloudy\\'}', 'Assistant Reply: {\\n    \"thoughts\": {\\n        \"text\": \"I have retrieved the necessary weather information for Shenzhen, China today. Now, I should proceed to write a weather report in Chinese for Shenzhen based on the data I obtained.\",\\n        \"reasoning\": \"Writing the weather report in Chinese will fulfill the task requirement and provide accurate information to the users in Shenzhen.\",\\n        \"plan\": [\\n            \"Write a weather report in Chinese for Shenzhen based on the retrieved weather data.\"\\n        ],\\n        \"criticism\": \"None\",\\n        \"speak\": \"I will now proceed to write a weather report in Chinese for Shenzhen based on the information I have gathered.\"\\n    },\\n    \"command\": {\\n        \"name\": \"write_file\",\\n        \"args\": {\\n            \"file_path\": \"weather_report_shenzhen_chinese.txt\",\\n            \"text\": \"深圳天气报告（今日）\\\\n\\\\n日期：星期六\\\\n天气：多云\\\\n温度：86华氏度\\\\n降水概率：0%\\\\n湿度：72%\\\\n风速：12英里/小时\\\\n\\\\n深圳，广东省，中国\",\\n            \"append\": false\\n        }\\n    }\\n} \\nResult: Command write_file returned: File written successfully to weather_report_shenzhen_chinese.txt.']\n",
      "\n",
      "\n",
      "Human: Determine which next command to use, and respond using the format specified above:\n",
      "AI: {\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"I should start by searching for the weather in Shenzhen, China today to gather the necessary information for the weather report.\",\n",
      "        \"reasoning\": \"This will provide me with the most up-to-date information to create an accurate weather report.\",\n",
      "        \"plan\": [\n",
      "            \"Search for the weather in Shenzhen, China today.\"\n",
      "        ],\n",
      "        \"criticism\": \"None\",\n",
      "        \"speak\": \"I will begin by searching for the weather in Shenzhen, China today.\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"search\",\n",
      "        \"args\": {\n",
      "            \"tool_input\": \"weather in Shenzhen, China today\"\n",
      "        }\n",
      "    }\n",
      "}\n",
      "System: Command search returned: {'type': 'weather_result', 'temperature': '86', 'unit': 'Fahrenheit', 'precipitation': '0%', 'humidity': '72%', 'wind': '12 mph', 'location': 'Shenzhen, Guangdong Province, China', 'date': 'Saturday', 'weather': 'Mostly cloudy'}\n",
      "Human: Determine which next command to use, and respond using the format specified above:\n",
      "AI: {\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"I have retrieved the necessary weather information for Shenzhen, China today. Now, I should proceed to write a weather report in Chinese for Shenzhen based on the data I obtained.\",\n",
      "        \"reasoning\": \"Writing the weather report in Chinese will fulfill the task requirement and provide accurate information to the users in Shenzhen.\",\n",
      "        \"plan\": [\n",
      "            \"Write a weather report in Chinese for Shenzhen based on the retrieved weather data.\"\n",
      "        ],\n",
      "        \"criticism\": \"None\",\n",
      "        \"speak\": \"I will now proceed to write a weather report in Chinese for Shenzhen based on the information I have gathered.\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"write_file\",\n",
      "        \"args\": {\n",
      "            \"file_path\": \"weather_report_shenzhen_chinese.txt\",\n",
      "            \"text\": \"深圳天气报告（今日）\\n\\n日期：星期六\\n天气：多云\\n温度：86华氏度\\n降水概率：0%\\n湿度：72%\\n风速：12英里/小时\\n\\n深圳，广东省，中国\",\n",
      "            \"append\": false\n",
      "        }\n",
      "    }\n",
      "}\n",
      "System: Command write_file returned: File written successfully to weather_report_shenzhen_chinese.txt.\n",
      "Human: Determine which next command to use, and respond using the format specified above:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "{\n",
      "    \"thoughts\": {\n",
      "        \"text\": \"I have successfully completed the task of writing the weather report in Chinese for Shenzhen, China today. It seems that all objectives have been accomplished.\",\n",
      "        \"reasoning\": \"The weather report has been written and saved, fulfilling the task requirements.\",\n",
      "        \"plan\": [\n",
      "            \"Finish and submit the completion of all objectives.\"\n",
      "        ],\n",
      "        \"criticism\": \"None\",\n",
      "        \"speak\": \"I have completed all tasks successfully.\"\n",
      "    },\n",
      "    \"command\": {\n",
      "        \"name\": \"finish\",\n",
      "        \"args\": {\n",
      "            \"response\": \"final response to let people know you have finished your objectives\"\n",
      "        }\n",
      "    }\n",
      "}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'final response to let people know you have finished your objectives'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.run(goals=['Write a weather report in Chinese for Shenzhen, China today'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Chat History Memory\n",
    "\n",
    "In addition to the memory that holds the agent immediate steps, we also have a chat history memory. By default, the agent will use 'ChatMessageHistory' and it can be changed. This is useful when you want to use a different type of memory for example 'FileChatHistoryMemory'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.chat_message_histories import FileChatMessageHistory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nagent = AutoGPT.from_llm_and_tools(\\n    ai_name=\"Jarvis\",  # J.A.R.V.I.S - Just A Rather Very Intelligent System\\n    ai_role=\"Assistant\",\\n    tools=tools,\\n    llm=ChatOpenAI(temperature=0),\\n    memory=faiss_db.as_retriever(),\\n    chat_history_memory=FileChatMessageHistory(\"chat_history.txt\"),\\n)\\n'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "agent = AutoGPT.from_llm_and_tools(\n",
    "    ai_name=\"Jarvis\",  # J.A.R.V.I.S - Just A Rather Very Intelligent System\n",
    "    ai_role=\"Assistant\",\n",
    "    tools=tools,\n",
    "    llm=ChatOpenAI(temperature=0),\n",
    "    memory=faiss_db.as_retriever(),\n",
    "    chat_history_memory=FileChatMessageHistory(\"chat_history.txt\"),\n",
    ")\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"\\nagent.run(goals=['Give me some LangChain learning websites and save them to a file'])\\n\""
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "agent.run(goals=['Give me some LangChain learning websites and save them to a file'])\n",
    "'''"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
